var app = require("lzbjpk_app");

/**
 * 类构造
 */
var BJPKRoom = app.BaseClass.extend({

    /**
     * 初始化
     */
    Init: function () {

        this.JS_Name = "BJPKRoom";

        this.ComTool = app[app.subGameName + "_ComTool"]();
        this.ShareDefine = app[app.subGameName + "_ShareDefine"]();
        this.HeroManager = app[app.subGameName + "_HeroManager"]();

        this.BJPKRoomPosMgr = app[app.subGameName.toUpperCase() + "RoomPosMgr"]();
        this.BJPKRoomSet = app[app.subGameName.toUpperCase() + "RoomSet"]();
        this.SysDataManager = app[app.subGameName + "_SysDataManager"]();

        this.PropertyInfo = this.SysDataManager.GetTableDict("PropertyInfo");

        this.OnReload();

        console.log("Init");

    },

    OnReload: function () {

        //{
        //	"roomID":1,
        //	"key":"123456",
        //	"createSec":nowSec,
        //	"state":roomState,
        //	"setID":0,
        //	"roomEnd":{},
        //	"dissolve":{"endSec":0,"createPos":0,"posAgreeList":[]},
        //}
        this.dataInfo = {};

        //{
        //	"setCount":4,
        //}
        this.roomConfig = {};

        this.roomRecord = {};

        this.BJPKRoomPosMgr.OnReload();
        this.BJPKRoomSet.OnReload();
    },
    UpdateOwnerID: function (ownerID) {
        this.dataInfo['ownerID'] = ownerID;
    },
    //-----------------------回调函数-----------------------------
    //登录初始化房间数据
    OnInitRoomData: function (serverPack) {
        this.BJPKRoomMgr = app[app.subGameName.toUpperCase() + "RoomMgr"]();

        let room = this.BJPKRoomMgr.GetEnterRoom();
        serverPack["state"] = this.ShareDefine.RoomStateStringDict[serverPack["state"]];

        if (serverPack["prizeType"] == 'Gold') {
            app[app.subGameName + "_ShareDefine"]().isCoinRoom = true;
        }
        else if (serverPack["prizeType"] == 'RoomCard') {
            app[app.subGameName + "_ShareDefine"]().isCoinRoom = false;
        }
        if (serverPack["createType"] == 3) {
            app[app.subGameName + "_ShareDefine"]().isClubRoom = true;
        }
        if (serverPack["createType"] == 4) {
            app[app.subGameName + "_ShareDefine"]().IsRaceRoom = true;
        }

        let cfg = serverPack["cfg"];
        this.roomConfig = cfg;
        //更新cfg中的createType值
        this.roomConfig["createType"] = serverPack["createType"];

        let roomPosInfoList = serverPack["posList"] || [];
        this.BJPKRoomPosMgr.OnInitRoomPosData(roomPosInfoList);

        let setInfo = serverPack["set"];
        this.BJPKRoomSet.OnInitRoomSetData(setInfo);

        this.isGetGR = false;
        //其余信息存放到dataInfo
        this.dataInfo = serverPack;
        console.log("roomConfig:", this.roomConfig);
        console.log("dataInfo:", this.dataInfo);
    },

    //位置离开
    OnPosLeave: function (pos) {
        this.BJPKRoomPosMgr.OnPosLeave(pos);
        this.BJPKRoomSet.OnPosLeave(pos);
    },

    //继续游戏
    OnPosContinueGame: function (pos) {
        this.BJPKRoomPosMgr.OnPosContinueGame(pos);
        this.BJPKRoomSet.OnPosContinueGame(pos);
    },

    //set开始
    OnSetStart: function (setInfo) {
        this.dataInfo["state"] = this.ShareDefine.RoomState_Playing;
        this.dataInfo["setID"] = setInfo["setID"];
        this.GetRoomSet().OnSetStart(setInfo);
    },
    UpdateEndSec: function (endSec) {
        this.dataInfo["dissolve"]["endSec"] = endSec;
    },
    //位置同意拒绝更新
    OnPosDealVote: function (pos, agreeDissolve) {

        let dissolveInfo = this.dataInfo["dissolve"];
        let posAgreeList = dissolveInfo["posAgreeList"];
        if (!posAgreeList) {
            console.error("OnPosDealVote not find posAgreeList:", this.dataInfo);
            return
        }

        if (pos >= posAgreeList.length) {
            console.error("OnPosDealVote(%s,%s):", pos, agreeDissolve, posAgreeList);
            return
        }

        if (agreeDissolve) {
            posAgreeList[pos] = 1;
        }
        else {
            posAgreeList[pos] = 2;
        }
        return dissolveInfo
    },
    //有玩家加入更新玩家数据
    OnSetPosData: function (pos, posInfo) {
        if (this.dataInfo["posList"]) {
            for (let i = 0; i < this.dataInfo["posList"].length; i++) {
                if (pos == this.dataInfo["posList"][i]["pos"]) {
                    this.dataInfo["posList"][i] = posInfo;
                }
            }
        }
    },

    //set结束
    OnSetEnd: function (setEnd) {
        this.BJPKRoomSet.OnSetEnd(setEnd);
        this.BJPKRoomPosMgr.OnSetEnd(setEnd);
    },
    //人数修改
    OnStartChangePlaeryDissolve: function (createPos, playerNum, endSec) {
        let posAgreeList = [];
        for (let index = 0; index < 5; index++) {
            if (index == createPos) {
                posAgreeList.push(1);
            } else {
                posAgreeList.push(0);
            }
        }
        let dissolveInfo = {"endSec": endSec, "createPos": createPos, "posAgreeList": posAgreeList};
        this.dataInfo["changePlayerNum"] = dissolveInfo;
        return dissolveInfo;
    },

    //位置同意拒绝更新
    ChangePlayerNumAgree: function (pos, agreeDissolve) {
        let dissolveInfo = this.dataInfo["changePlayerNum"];
        let posAgreeList = dissolveInfo["posAgreeList"];
        if (!posAgreeList) {
            console.error("OnChangeDealVote not find posAgreeList:", this.dataInfo);
            return
        }
        if (pos >= posAgreeList.length) {
            console.error("OnChangeDealVote(%s,%s):", pos, agreeDissolve, posAgreeList);
            return
        }
        if (agreeDissolve) {
            posAgreeList[pos] = 1;
        }
        else {
            posAgreeList[pos] = 2;
        }
        return dissolveInfo
    },
    //房间结束
    OnRoomEnd: function (roomEnd) {
        this.dataInfo["state"] = this.ShareDefine.RoomState_End;
        this.dataInfo["roomEnd"] = roomEnd;
    },

    //开始解散房间
    OnStartVoteDissolve: function (createPos, endSec) {
        let posAgreeList = [];
        for (let index = 0; index < 5; index++) {
            if (index == createPos) {
                posAgreeList.push(1);
            }
            else {
                posAgreeList.push(0);
            }
        }
        let dissolveInfo = {"endSec": endSec, "createPos": createPos, "posAgreeList": posAgreeList};
        this.dataInfo["dissolve"] = dissolveInfo;
        return dissolveInfo;
    },
    //更新房间内的对局记录信息
    RoomRecord: function (serverPack) {
        this.roomRecord = {};
        this.roomRecord = serverPack;
    },
    //更新Room datainfo数据
    UpdataInfo: function (serverPack) {
        return this.GetRoomSet().OnPosOpCard(serverPack)
    },
    //---------------------设置函数---------------------

    //---------------------获取函数---------------------
    //获取对局记录信息
    GetRoomRecord: function () {
        return this.roomRecord;
    },
    //获取房间信息
    GetRoomDataInfo: function () {
        return this.dataInfo;
    },
    //获取创建房间信息b
    GetRoomProperty: function (property) {
        if (!this.dataInfo.hasOwnProperty(property)) {
            console.error("GetRoomProperty not find:%s", property);
            return
        }
        return this.dataInfo[property];
    },
    ClearDissolve: function () {
        this.dataInfo['dissolve'] = '';
    },
    ClearchangePlayerNum: function () {
        this.dataInfo['changePlayerNum'] = '';
    },
    //获取房间配置信息
    GetRoomConfig: function () {
        return this.roomConfig
    },

    //获取房间配置信息
    GetRoomConfigByProperty: function (property) {
        if (!this.roomConfig.hasOwnProperty(property)) {
            console.error("GetRoomConfigByProperty not find:%s", property);
            return
        }
        return this.roomConfig[property];
    },
    OnSportsPointChange: function (serverPack) {
        this[app.subGameName.toUpperCase() + "RoomPosMgr"].OnSportsPointChange(serverPack);
    },
    GetRoomSet: function () {
        let setID = this.dataInfo["setID"];
        if (!setID) {
            console.error("GetSet not start set");
            return
        }
        return this.BJPKRoomSet
    },

    GetRoomPosMgr: function () {
        return this.BJPKRoomPosMgr
    },
    //客户端玩家是否是开房人
    IsClientIsCreater: function () {
        let heroID = this.HeroManager.GetHeroID();
        if (heroID == this.dataInfo["createID"]) {
            return true
        }
        return false
    },
    //客户端玩家是否是房主
    IsClientIsOwner: function () {
        let heroID = this.HeroManager.GetHeroID();
        if (heroID == this.dataInfo["ownerID"]) {
            return true
        }
        return false
    },
    //获取玩家setPos对象
    GetClientPlayerSetPos: function () {

        let pos = this.BJPKRoomPosMgr.GetClientPos();
        if (pos < 0) {
            console.error("GetClientPlayerSetPos not enter room");
            return
        }
        let setPos = this.BJPKRoomSet.GetSetPosByPos(pos);
        if (!setPos) {
            console.error("GetClientPlayerSetPos(%s) not find setPos", pos);
            return
        }
        return setPos
    },
    SetGameRecord: function (bget) {
        this.isGetGR = bget;
    },

    GetGameRecord: function () {
        return this.isGetGR;
    },
    //获取客户端玩家信息
    GetClientPlayerInfo: function () {
        let pos = this.BJPKRoomPosMgr.GetClientPos();
        if (pos < 0) {
            console.error("GetClientPlayerInfo not enter room");
            return
        }
        let playerInfo = this.BJPKRoomPosMgr.GetPlayerInfoByPos(pos);
        if (!playerInfo) {
            console.error("GetClientPlayerInfo(%s) not find playerInfo", pos);
            return
        }
        return playerInfo
    },
});

var g_BJPKRoom = null;

/**
 * 绑定模块外部方法
 */
exports.GetModel = function () {
    if (!g_BJPKRoom)
        g_BJPKRoom = new BJPKRoom();
    return g_BJPKRoom;
};